package com.mmd.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.mmd.bean.AdminInfoBean;
import com.mmd.service.impl.AjaxDemoServiceImpl;
import com.mmd.utils.Myutils;

/**
 * Servlet implementation class AjaxDemoAction
 */
@WebServlet("/AjaxDemoAction")
public class AjaxDemoAction extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * 对客户端请求进行响应处理
	 */		@Override
		protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			//获取表单数据
			AdminInfoBean adminB=Myutils.convertFormDate2Bean(request, AdminInfoBean.class);
			System.out.println("adminB="+adminB);
			//进行查询操作
			List<AdminInfoBean> lstAdmins=Myutils.getInstance(AjaxDemoServiceImpl.class).queryLoginByParams(adminB);
			System.out.println("查询结果："+lstAdmins);
			request.getSession().setAttribute("Host_id", lstAdmins.get(0).getHost_id());//缓存登录成功的用户信息
			request.getSession().setAttribute("Host_name()", lstAdmins.get(0).getHost_name());
			System.out.println("获取用户id:"+request.getSession().getAttribute("Host_id"));
			//ajax响应
			//服务器向客户端发送数据，把字符串对象（也就是list集合）转换为json对象
			String strJson1=JSON.toJSONString(lstAdmins);
			System.out.println("xiaolalallala");
			//响应客户端的请求
			 PrintWriter pw=response.getWriter();
			 //将json字符串发回给客户端浏览器 
			 
			 pw.write(strJson1);
			 pw.flush();
	         pw.close();
			
		}
}
